package io.dcloud.uniplugin;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;


import androidx.constraintlayout.widget.Group;

import uni.dcloud.io.uniplugin_module.R;

public class NativePageActivity extends Activity {

    private CameraPreview preview;
    private ImageView close_iv;
    private ImageView light_iv;
    private ImageView take_iv;
    private ImageView unselect_iv;
    private ImageView select_iv;
    private Button set_zoom_bt;
    private Group ids_gr;
    /**
     * 相机预览
     */
    private FrameLayout mPreviewLayout;

    /**
     * 相机类
     */
    private Camera mCamera;

    /**
     * 图片流暂存
     */
    private byte[] imageData;

    /**
     * 拍照标记
     */
    private boolean isTakePhoto = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_native);
        mPreviewLayout = findViewById(R.id.camera_preview_layout);
        close_iv = findViewById(R.id.close_iv);
        take_iv = findViewById(R.id.take_iv);
        unselect_iv = findViewById(R.id.unselect_iv);
        select_iv = findViewById(R.id.select_iv);
        set_zoom_bt = findViewById(R.id.set_zoom_bt);
        light_iv = findViewById(R.id.light_iv);
        ids_gr = findViewById(R.id.ids_gr);
//        FrameLayout rootView = new FrameLayout(this);
//        TextView textView = new TextView(this);
//        textView.setTextColor(Color.BLACK);
//        textView.setTextSize(30);
//        textView.setText("点击我将返回 并携带参数返回");
//        rootView.addView(textView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));
//        textView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.putExtra("respond", "我是原生页面");
//                setResult(TestModule.REQUEST_CODE, intent);
//                finish();
//            }
//        });
//        setContentView(rootView);
    }

    /**
     * 注释：设置监听事件
     */
    private void setOnclickListener() {

        close_iv.setOnClickListener(v -> {
            finish();
        });
        take_iv.setOnClickListener(v -> {
            if (!isTakePhoto) {
                takePhoto();
            }
        });
        unselect_iv.setOnClickListener(v -> {
            take_iv.setVisibility(View.VISIBLE);
            ids_gr.setVisibility(View.GONE);
            ids_gr.requestLayout();
//            cancelSavePhoto();
        });

        select_iv.setOnClickListener(v -> {
            unselect_iv.setVisibility(View.GONE);
            select_iv.setVisibility(View.GONE);
            ids_gr.requestLayout();
//            showDialogProgress("图片处理中···");
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
//                    savePhoto();
                }
            };
            ThreadPoolInstance.getInstance().getThreadPoolExecutor().execute(runnable);
        });

        /**
         * 变焦
         */
        set_zoom_bt.setOnClickListener(v -> {
            boolean isS = preview.isSupportZoom();
            if (isS) {
                preview.setZoom();
            }
        });

        /**
         * 补光灯
         */
        light_iv.setOnClickListener(v -> {
            Camera.Parameters mParameters = mCamera.getParameters();

            // 打开补光灯
            mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(mParameters);
        });
    }

    /**
     * 注释：拍照并保存图片到相册
     */
    private void takePhoto() {
        System.gc();
        isTakePhoto = true;
        //调用相机拍照
        mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                imageData = data;
                //停止预览
                mCamera.stopPreview();
            }
        });
        mLoopHandler.postDelayed(mLoopRunnable, 0);
    }

    private Handler mLoopHandler = new Handler();
    private Runnable mLoopRunnable = new Runnable() {
        @Override
        public void run() {
            int size = imageData.length;
            if (size > 0) {
                take_iv.setVisibility(View.GONE);
                ids_gr.setVisibility(View.VISIBLE);
                mLoopHandler.removeCallbacks(this);
            } else {
                mLoopHandler.postDelayed(this, 10);
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        mCamera = Camera.open(0);
        Log.d("预览数据", "开始预览");
        preview = new CameraPreview(this, mCamera);
        mPreviewLayout.addView(preview);
        mCamera.setOneShotPreviewCallback((data, camera) -> {
            if (data != null && data.length > 0) {
                Log.d("预览数据", "收到预览数据");
            }
        });
    }
    @Override
    protected void onPause() {
        super.onPause();
        preview.closeCamera();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        preview.destroyDrawingCache();//释放资源
    }
}
